package cn.jiyun.service.impl;

import cn.jiyun.common.R;
import cn.jiyun.dao.SetmealDao;
import cn.jiyun.dao.SetmealDishDao;
import cn.jiyun.pojo.SC;
import cn.jiyun.pojo.SetmealDish;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class SetmealSerivce implements cn.jiyun.service.SetmealSerivce {

    @Resource
    private SetmealDao setmealDao;



    @Resource
    private SetmealDishDao setmealDishDao;

    @Override
    public R page(Page<SC> page1, String name) {
        Page<SC> setmealPage = setmealDao.selectPagecs(page1, name);
        return R.success(setmealPage);
    }

    @Override
    public void status1(Long id) {
        setmealDao.status1(id);
    }

    @Override
    public void status0(Long id) {
        setmealDao.status0(id);
    }

    @Override
    public void del(Long id) {
        setmealDao.deleteById(id);
        setmealDishDao.deleteByIdsc(id);
    }

    @Override
    public Object add(SC cs, Long id) {
        cs.setCreateTime(LocalDateTime.now());
        cs.setUpdateTime(LocalDateTime.now());
        cs.setUpdateUser(id);
        cs.setCreateUser(id);
       setmealDao.insert(cs);
            List<SetmealDish> SetmealDish = cs.getSetmealDishes();
            for (cn.jiyun.pojo.SetmealDish ss : SetmealDish) {
                ss.setCreateTime(LocalDateTime.now());
                ss.setUpdateTime(LocalDateTime.now());
                ss.setUpdateUser(id);
                ss.setCreateUser(id);
                ss.setSetmealId(cs.getCategoryId());

                setmealDishDao.insert(ss);
            }



        return R.success("成功");
    }

    @Override
    public Object update(SC cs, Long id) {
        cs.setCreateTime(LocalDateTime.now());
        cs.setUpdateTime(LocalDateTime.now());
        cs.setUpdateUser(id);
        cs.setCreateUser(id);
        setmealDao.updateById(cs);
//        cs.getSetmealDishes(); 等于修改的表内数据
        List<SetmealDish> setmealDishes = cs.getSetmealDishes();
//        要删除的表

            setmealDishDao.deleteByIdsc(cs.getId());
        for (SetmealDish s : setmealDishes) {
            s.setCreateTime(LocalDateTime.now());
            s.setUpdateTime(LocalDateTime.now());
            s.setUpdateUser(id);
            s.setCreateUser(id);
            s.setSetmealId(cs.getId());
            setmealDishDao.insert(s);
        }



        return null;
    }

    @Override
    public SC hx(Long id) {
        SC sc=setmealDao.hx(id);
        List<SetmealDish>setmealDishes=setmealDishDao.selectListhx(id);
       sc.setSetmealDishes(setmealDishes);
        return sc;
    }


}
